package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.product.pojo.param.CategoryAddNewParam;
import cn.tedu.csmall.product.pojo.param.CategoryUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.CategoryListItemVO;
import cn.tedu.csmall.product.pojo.vo.CategoryTreeItemVO;
import cn.tedu.csmall.product.service.CategoryService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("category")
@Api(tags = "07.类别管理模块")
@Validated
public class CategoryController {

    @Autowired
    private CategoryService service;

    @PostMapping("add-new")
    @ApiOperation("添加类别")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid CategoryAddNewParam categoryAddNewParam) {

        service.addNew(categoryAddNewParam);

        return JsonResult.success();
    }

    @PostMapping("delete")
    @ApiOperation("根据id删除类别")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "categoryId", value = "类别ID", required = true, dataType = "long")
    public JsonResult delete(@Range(min = 1, message = "类别ID不合法！")
                             @RequestParam Long categoryId) {
        service.deleteById(categoryId);
        return JsonResult.success();
    }

    @PostMapping("updateInfoById")
    @ApiOperation("更改类别详情信息")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@RequestParam @Range(min = 1, message = "请提交有效的类别id") Long id,
                                     @Valid CategoryUpdateInfoParam categoryUpdateInfoParam) {
        service.updateInfoById(id,categoryUpdateInfoParam);
        return JsonResult.success();
    }

    @GetMapping("list-by-parent")
    @ApiOperation("根据父级类别查询子级类别列表")
    @ApiOperationSupport(order = 400)
    @PreAuthorize("hasAuthority('/pms/category/read')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父级ID", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "page", value = "页码值", defaultValue = "1", paramType = "query", dataType = "int")
    })
    public JsonResult list(@Range(message = "请提交有效的父级类别ID值！") Long parentId,
                           @Range(min = 1, message = "请提交有效的页码值！") Integer page) {
        log.debug("开始处理【根据父级类别查询子级类别列表】的请求，父级类别：{}，页码：{}", parentId, page);
        Integer pageNum = page == null ? 1 : page;
        PageData<CategoryListItemVO> pageData = service.listByParentId(parentId, pageNum);
        return JsonResult.success(pageData);
    }

    @GetMapping("getStandardById/{id}")
    @ApiOperation("根据ID查询类别")
    @ApiOperationSupport(order = 410)
    @PreAuthorize("hasAuthority('/pms/category/read')")
    @ApiImplicitParam(name = "id", value = "类别ID", required = true, paramType = "query", dataType = "long")
    public JsonResult getStandardById(@PathVariable @Range(min = 1, message = "请提交有效的类别id") Long id) {
        return JsonResult.success(service.getStandardById(id));
    }

    @PostMapping("{id:[0-9]+}/setEnable")
    @PreAuthorize("hasAuthority('/pms/category/update')")
    @ApiOperation("设置启用或者禁用")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "enable", value = "是否启用", required = true, dataType = "int"),
    })
    public JsonResult setEnable(@PathVariable Long id,
                                @RequestParam @Range(max = 1, message = "设置是否启用失败，请输入0：禁用；1：启用") Integer enable) {
        service.setEnable(id, enable);
        return JsonResult.success();
    }

    @PostMapping("{id:[0-9]+}/setIsDisplay")
    @PreAuthorize("hasAuthority('/pms/category/update')")
    @ApiOperation("设置展示或者不展示")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "isDisplay", value = "是否展示", required = true, dataType = "int"),
    })
    public JsonResult setIsDisplay(@PathVariable Long id,
                                   @RequestParam @Range(max = 1, message = "设置是否展示失败，请输入0：不展示；1：展示") Integer isDisplay) {
        service.setIsDisplay(id, isDisplay);
        return JsonResult.success();
    }

    @GetMapping("tree")
    @PreAuthorize("hasAuthority('/pms/category/read')")
    @ApiOperation("查询类别树")
    @ApiOperationSupport(order = 440)
    public JsonResult listTree() {
        List<CategoryTreeItemVO> categoryTree = service.listTree();
        return JsonResult.success(categoryTree);
    }

}
